package sd.czw.com.mmgapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalCallback;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalModel;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/15.
 */
public class ResetPasswordActivity extends AppCompatActivity {


    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.getphonenumber)
    Button getphonenumber;
    @Bind(R.id.edt_one)
    EditText edtOne;
    @Bind(R.id.edt_two)
    EditText edtTwo;
    private String phonenumber;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset);
        ButterKnife.bind(this);
        phonenumber = getIntent().getStringExtra("phonenumber");
        edtOne.addTextChangedListener(watcher);
        edtTwo.addTextChangedListener(watcher);
    }

    @OnClick({R.id.rl_back, R.id.getphonenumber})
    void onClicl(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                onBackPressed();
                break;
            case R.id.getphonenumber:
                //startActivity(new Intent(this,LoginActivity.class));
                String trim = edtOne.getText().toString().trim();
                String trim1 = edtTwo.getText().toString().trim();
                if (trim.equals(trim1)){
                    OkHttpUtils.post().url(Contants.NMLOGIN).addParams("mobile_phone",phonenumber)
                            .addParams("password",trim).addParams("confirm_password",trim1)
                            .addParams("act","send_pwd_sms").addParams("time",Contants.Time)
                            .addParams("sign",Contants.Sign)
                            .build().execute(new LoginNormalCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(LoginNormalModel response, int id) {
                            if (response.getErrcode() == 1){
                                Toast.makeText(ResetPasswordActivity.this, "密码重置成功", Toast.LENGTH_SHORT).show();
                                ResetPasswordActivity.this.startActivity(new Intent(ResetPasswordActivity.this,LoginActivity.class));
                            }else {
                                Toast.makeText(ResetPasswordActivity.this, ""+response.getErrmsg(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else {
                    Toast.makeText(ResetPasswordActivity.this, "两次密码输入不一样", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String s = edtOne.getText().toString();
            String s1 = edtTwo.getText().toString();
            if (s != null && s1 != null && !("".equals(s)) && !("".equals(s1))){
                getphonenumber.setBackground(getDrawable(R.drawable.login_checkbtn));
            }else {
                getphonenumber.setBackground(getDrawable(R.drawable.login_btn));
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
}
